package com.service.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.beans.Admin;
import com.beans.User;
import com.mapper.UserMapper;
import com.service.UserService;
import org.springframework.util.StringUtils;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    public User login(String userName, String password) {
        return userMapper.login(userName, password);
    }

    public Admin loging(String userName, String password) {
        return userMapper.loging(userName, password);
    }

    public int register(User user) {
        return userMapper.register(user);
    }

    public List<User> get(String userName) {
        return userMapper.get(userName);
    }

    @Override
    public Map<String, Object> queryByPage(User user, Integer page, Integer limit) {
        Map<String, Object> resMap = new HashMap<>(4);
        PageHelper.offsetPage((page - 1) * limit, limit);
        List<User> dataList =  Collections.EMPTY_LIST;;
        if(StringUtils.hasText(user.getUserName())){
            dataList = userMapper.get(user.getUserName());
        }else{
            dataList = userMapper.getAllUser();
        }

        PageInfo<User> pageInfo = new PageInfo<>(dataList);
        resMap.put("code", 0);
        resMap.put("msg", "查询成功");
        resMap.put("data", pageInfo.getList());
        resMap.put("count", pageInfo.getTotal());
        return resMap;
    }

    public int addUser(User user) {
        return userMapper.addUser(user);
    }

    public int deleteUser(int id) {
        return userMapper.deleteUser(id);
    }

    public User getUserById(int id) {
        return userMapper.getUserById(id);
    }

    public List<User> getAllUser() {
        return userMapper.getAllUser();
    }

    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }
}

